<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>平台公司管理</title>
    <link rel="stylesheet" type="text/css" href="/themes/default/easyui.css" id="swicth-style">
    <link rel="stylesheet" type="text/css" href="/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/css/common.css">
    <link rel="stylesheet" type="text/css" href="/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/themes/color.css">
    <link rel="stylesheet" type="text/css" href="/js/uploadify-v3.2.1/uploadify.css">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script type="text/javascript" src="/js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/jquery.cookie.js"></script>
    <script type="text/javascript" src="/js/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/js/common.js"></script>
    <script type="text/javascript" src="/js/uploadify-v3.2.1/jquery.uploadify.min.js"></script>
    <script type="text/javascript" src="/js/uploadUtil.js"></script>
</head>

<body class="easyui-layout" style="padding:5px">
<fieldset>
    <legend>信息查询</legend>
    <form id="ffSearch" method="post">
        <div id="searchBar">
            <table cellspacing="0" cellpadding="0">
                <tr>
                    <th>统一社会信用代码：</th>
                    <td style="width:120px"><input  id="searchType" class="textbox" type="hidden" name="type" value="0" />
                        <input name="socialCode" class="easyui-textbox" type="text" style="width:100px">
                    </td>
                    <th>公司名称：</th>
                    <td style="width:120px">
                        <input name="name" class="easyui-textbox" type="text" style="width:100px">
                    </td>
                    <th>公司地址：</th>
                    <td style="width:120px">
                        <input name="address" class="easyui-textbox" type="text" style="width:100px">
                    </td>
                    <td colspan="2">
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="onSearch()">查询</a>&nbsp;&nbsp;
                        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="onClear()">重置</a>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</fieldset>

<table id="dg"> </table>

<div id="addOrEdit" class="easyui-window" title="添加平台公司" data-options="closed:true,iconCls:'icon-save',footer:'#ft'" style="width:600px;height:600px;padding:10px;">
    <form id="ff" method="post">
        <table class="table-blue">
            <tr>
                <th>公司类型：</th>
                <td><input id = "addType" name="type"  value="0" type="hidden"/>
                <input disabled="true" id="addTypeName" class="textbox" value="平台公司" data-options="required:true, validType:'maxLength[50]'"/>
                </td>
            </tr>
            <tr>
                <th>公司名称：</th>
                <td><input type="hidden" name="id"/><input class="easyui-validatebox textbox" type="text" name="name" data-options="required:true, validType:'maxLength[50]'"/>
                </td>
            </tr>
            <tr>
                <th>统一社会信用代码：</th>
                <td><input type="hidden" name="leader"/><input class="easyui-validatebox textbox" type="text" name="socialCode" data-options="required:true, validType:'maxLength[18]'"/>
                </td>
            </tr>
            <tr>
                <th>公司地址：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="address" data-options="required:true, validType:'maxLength[18]'"/>
                </td>
            </tr>
            <tr>
                <th>负责人：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="leaderName" data-options="required:true, validType:'maxLength[30]'"/></td>
            </tr>
            <tr>
                <th>手机号：</th>
                <td><input class="easyui-validatebox textbox" type="text" id="mobile" name="mobile" data-options="required:true, validType:['mobile','maxLength[11]']" /></td>
            </tr>
            <tr>
                <th>邮箱：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="email" data-options="prompt:'Enter a valid email.',validType:['email','maxLength[45]']"/></td>
            </tr>
            <tr>
                <th>银行卡号：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="bankCardNumber" data-options="required:true,validType:['maxLength[50]']"/></td>
            </tr>
            <tr>
                <th>支付宝账号：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="alipayAccount" data-options="validType:['maxLength[19]']"/></td>
            </tr>
            <tr>
                <th>微信账号：</th>
                <td><input class="easyui-validatebox textbox" type="text" name="wechatAccount" data-options="validType:['maxLength[50]']"/></td>
            </tr>
            <tr>
                <th style="vertical-align: middle;padding-top: 10px;">营业执照：</th>
                <td class="inputTd" >
                    <div style="float: left;">
                        <input type="hidden" name="licensePath" id="licensePath" value=""/>
                        <img alt="" src="/images/default.jpg" width="200" height="100" id="staticInfoImageShow">
                        <br>
                        <div style="float:left;margin-right: 40px">
                            <input type="file" name="staticInfoImage" id="staticInfoImage" />
                        </div>
                       <div class="uploadify-button " id="deleteStaticInfoImageBtn" onclick="deleteStaticInfoImg();" style="line-height: 22px; width: 80px; height: 22px;cursor:pointer;float: left;display:none">
                        <span class="uploadify-button-text">
						删除图片
						</span>
                        </div>
                    </div>
                </td>
            </tr>
           <!-- <tr>
                <th>备注：</th>
                <td><input class="easyui-validatebox textbox" name="remarks" data-options="multiline:true, validType:'maxLength[60]'" style="height:60px;"/></td>
            </tr>-->
        </table>
    </form>
    <div id="ft" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="submitForm()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>
<div id="showImageId" class="easyui-window" title="营业执照" data-options="closed:true" style="width:600px;height:400px;padding:10px;">
    <div style="float: left;">
        <img alt="" src="/images/default.jpg" width="100%" height="100%" id="showCompanyImageId">
    </div>
</div>
<div id="bind" class="easyui-window" title="绑定角色" data-options="closed:true,iconCls:'icon-save',footer:'#ft1'" style="width:300px;height:500px;padding:10px;">
    <div id="dl"></div>
    <div id="ft1" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="bindRole()" style="width:80px">Ok</a> &nbsp;&nbsp;
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Cancel</a>
    </div>
</div>

<div id="showDetail"  class="easyui-window" style="width:70%;height:80%;padding:10px;" title="查看详情" data-options="closed:true,iconCls:'icon-save'">
    <div id="detail" >
        <table id="detail_table" class="table-blue"  style="width:98%" >
            <tr style="height:30px;">
                <th>公司名称：</th><td align="center"><span id="detail_name"></span></td><th style="width:15%">统一社会信用代码：</th><td align="center"><span id="detail_socialCode">null</span></td>
            </tr>
            <tr style="height:30px;">
                <th>公司地址：</th><td align="center"><span id="detail_address"></span></td><th>公司责任人名称：</th><td align="center"><span id="detail_leaderName">null</span></td>
            </tr>
            <tr style="height:30px;">
                <th>公司电话：</th><td align="center"><span id="detail_mobile"></span></td><th>公司邮箱：</th><td align="center"><span id="detail_email">null</span></td>
            </tr>
            <tr style="height:30px;">
                <th>银行卡号：</th><td align="center"><span id="detail_bankCardNumber"></span></td><th>支付宝账号：</th><td align="center"><span id="detail_alipayAccount">null</span></td>
            </tr>
            <tr style="height:30px;">
                <th>微信账号：</th><td align="center"><span id="detail_wechatAccount"></span></td><th>创建时间：</th><td align="center"><span id="detail_createTime">null</span></td>
            </tr>
            <!--<tr style="height:30px;">-->
                <!--<th>备注：</th><td colspan="3"><span id="detail_remark"></span></td>-->
            <!--</tr>-->
            <tr >
                <th>营业执照：</th><td colspan="3" align="center">
                <div style="width:90%;height:340px">
                    <img alt="" src="/images/default.jpg" width="90%" id="detail_license" style="height:340px">
                </div>
            </td>
            </tr>
        </table>
    </div>
    <div id="detail_close" style="padding:5px;text-align: right;">
        <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="closeWindow()" style="width:80px">Close</a>
    </div>
</div>

<script type="text/javascript">
    $(function(){
        var dg = $('#dg').datagrid();
        var queryParams = $('#ffSearch').serializeObject();
        dg.datagrid({
            url:'/v1/api0/company/list',
            queryParams: queryParams,
            columns:[[
                {field:'id',checkbox:true},
                {field:'name',title:'公司名称',width:100},
                {field:'socialCode',title:'统一社会信用代码',width:100,
                 formatter:function (value, row, index) {
                 return '<a href="#" onclick="showRowImg(' + index + ')">'+row.socialCode+'</a>';
                }},
                {field:'leader',title:'负责人id',width:100,hidden:"true"},
                {field:'leaderName',title:'负责人名称',width:100},
                {field:'mobile',title:'手机号',width:100},
                {field:'email',title:'邮箱',width:100},
                {field:'createTime',title:'创建时间',width:100,formatter: formatDateTime},
                {field:'status',title:'状态',width:60,formatter: function(value,row,index){
                    switch(value) {
                        case 1: return "启用";
                        case 2: return "禁用";
                    }
                }},
                {field:'action',title:'操作',width:120,formatter: function(value,row,index){
                    if(row.status == 1){
                        return '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable(\''+row.id+'\',\''+row.leader+'\')">禁用</a> | ' +
                           '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="detail(\''+row.id+'\')">查看详情</a> | ' +
                            '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="resetPassword(\''+row.leader+'\')">重置密码</a>';
                    }else{  return '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable(\''+row.id+'\',\''+row.leader+'\')">启用</a> | ' +
                        '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="detail(\''+row.id+'\')">查看详情</a> | ' +
                            '<a href="javascript:void(0)" class="easyui-linkbutton" onclick="resetPassword(\''+row.leader+'\')">重置密码</a>';
                    }
                }}
            ]],
            title:'平台公司管理',
            pagination: true,
            iconCls: 'icon-application_view_detail',
            singleSelect: false,
            toolbar: [{
                id: 'btnAdd',
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            }, '-', {
                id: 'btnEdit',
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    edit();
                }
            }, '-', {
                id: 'btnDisable',
                text: '禁用',
                iconCls: 'icon-remove',
                handler: function () {
                    batchDisable();
                }
            }, '-', {
                id: 'btnEnable',
                text: '启用',
                iconCls: 'icon-add',
                handler: function () {
                    batchEnable();
                    }
            }, '-', {
                id: 'btnReload',
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    $("#dg").datagrid("reload");
                }
            }]
        });

        var heightMargin = $("#searchBar").height() + 45;
        $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        $(window).resize(function() {
            $('#dg').resizeDataGrid(heightMargin, 0, 0, 0);
        });
    });

    function onSearch(){
        var queryParams = $('#ffSearch').serializeObject();
        $('#dg').datagrid({
            url:'/v1/api0/company/list',
            queryParams: queryParams
        });
    }

    function onClear() {
        $('#ffSearch').form('clear');
        $('#statusSelect').combobox('setValue', '0');
        $("#searchType").val("0");//查询默认值保持平台公司
        onSearch();
    }

    function add() {
        $('#ff').form('clear');
        $("#addType").val("0");//类型为平台公司类型0，隐藏。
        $("#addTypeName").val("平台公司");//类型为平台公司，显示。
        deleteStaticInfoImg();
        $("#ff input[name='id']").val("");
        $('#addOrEdit').window({title:'添加公司'});
        $('#addOrEdit').window('open');
    }

    function edit() {
        $('#ff').form('clear');
        $("#addType").val("0");//类型为平台公司类型0，隐藏。
        $("#addTypeName").val("平台公司");//类型为平台公司，显示。
        var row = $('#dg').datagrid('getSelected');
        if (!row){
            top.window.showMessage("请选择公司");
            return;
        }
        getupdateImg(row.licensePath);
        $('#ff').form('load',row);
        $("#ff input[name='id']").val(row.id);
        $('body').data('ff-serialize', $('#ff').serialize());
        $('#addOrEdit').window({title:'编辑公司'});
        $('#addOrEdit').window('open');

    }

    function submitForm(){
        var id = $("#ff input[name='id']").val();
        var url = id.length ? "/v1/api0/company/edit" : "/v1/api0/company/add";
        var data = $('#ff').serialize();
        if(data == $('body').data('ff-serialize')) {
            top.window.showMessage("数据没有改动");
            return;
        }
        $.ajax({
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                if (!$('#ff').form('validate')){
                    return false;
                }
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        });
    }

    function closeWindow(){
        $('#addOrEdit').window('close');
        $('#bind').window('close');
        $('#showDetail').window('close');
    }


    function batchDisable() {
        var ids = [];
        var leaders = [];
        var rows = $('#dg').datagrid('getSelections');
        if(!rows.length) {
            top.window.showMessage("请选择公司");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
            leaders.push(rows[i].leader);
        }

        top.window.$.messager.confirm('确认', '您确定要禁用选择的公司吗？', function(r){
            if (r){
                var url = "/v1/api0/company/batchDisable";
                var data = {ids: ids.join(','),leaders:leaders.join(',')};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

    function batchEnable() {
        var ids = [];
        var leaders = [];
        var rows = $('#dg').datagrid('getSelections');
        if(!rows.length) {
            top.window.showMessage("请选择公司");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            ids.push(rows[i].id);
            leaders.push(rows[i].leader);
        }

        top.window.$.messager.confirm('确认', '您确定要启用选择的公司吗？', function(r){
            if (r){
                var url = "/v1/api0/company/batchEnable";
                var data = {ids: ids.join(','),leaders:leaders.join(',')};
                $.ajax(ajaxOption(url, data));
            }
        });
    }


    function disable(id,leader) {
     top.window.$.messager.confirm('确认', '您确定要禁用该公司吗？', function(r){
            if (r){
                 var url = "/v1/api0/company/disable";
                   var data = {id: id,leader:leader};
                    $.ajax(ajaxOption(url, data));
            }
        });
    }

    function enable(id,leader) {
     top.window.$.messager.confirm('确认', '您确定要启用该公司吗？', function(r){
            if (r){
                 var url = "/v1/api0/company/enable";
                 var data = {id: id,leader:leader};
                    $.ajax(ajaxOption(url, data));
            }
        });

    }
    /*密码重置*/
    function resetPassword(id) {
        top.window.$.messager.confirm('确认', '您确定要重置该公司负责人的密码吗？', function(r){
            if (r){
                var url = "/v1/api0/user/password/reset";
                var data = {id: id};
                $.ajax(ajaxOption(url, data));
            }
        });
    }

   /*之前的暂未修改*/
    function showBindWindow(id,leader) {
        $('body').data('companyId', id);
        $('body').data('leader', leader);
        $('#dl').datalist({
            url: '/v1/api0/role/list',
            valueField: 'id',
            textField: 'name',
            checkbox: true,
            singleSelect: false,
            rownumbers: false,
            onLoadSuccess:function(row) {
                var rowData = row.rows;
                $.ajax({
                    type: "post",
                    url: '/v1/api0/company/roles',
                    data: {companyId:id,leader:leader},
                    datatype: "json",
                    success: function (data) {
                        if(data.code != 200) {
                            return;
                        }
                        var roles = data.body;
                        $.each(rowData, function(idx,val){//遍历JSON
                            $.each(roles, function(i, role) {
                                if(val.id == role.id){
                                    $("#dl").datalist("selectRow", idx);//如果数据行为已选中则选中改行
                                    return;
                                }
                            });
                        });
                    }
                })
            }
        });
        $('#bind').window('open');
    }

    function bindRole() {
        var companyId = $('body').data('companyId');
        var leader = $('body').data('leader');


        var roleIds = [];
        var rows = $('#dl').datagrid('getSelections');
        if(!rows.length) {
            top.window.showMessage("请选择要绑定的角色");
            return;
        }
        for (var i = 0; i < rows.length; i++) {
            roleIds.push(rows[i].id);
        }

        var url = "/v1/api0/company/bindRole";
        var data = {companyId:companyId,leader:leader, roleIds: roleIds.join(',')};
        $.ajax(ajaxOption(url, data));
    }

    function batchBindRole() {
        var roleIds = [];
        var nodes = $('#tt').tree('getChecked', ['checked','indeterminate']);
        if(!nodes.length) {
            top.window.showMessage("请选择要绑定的角色");
            return;
        }
        for(var i=0; i<nodes.length; i++){
            roleIds.push(nodes[i].id);
        }
        var url = "/v1/api0/company/batchBindRole";
        var data = {companyIds: companyIds.join(','), roleIds: roleIds.join(',')};
        $.ajax(ajaxOption(url, data));
    }

    function ajaxOption(url, data) {
        return {
            type: "post",
            url: url,
            data: data,
            datatype: "json",
            beforeSend: function () {
                $('#dg').datagrid('loading');
            },
            success: function (data) {
                if(data.code == 200) {
                    $('#dg').datagrid('reload');
                    top.window.showMessage();
                    closeWindow();
                } else {
                    top.window.showMessage(data.msg);
                    $('#dg').datagrid('loaded');
                }
            },
            error: function () {
                $('#dg').datagrid('loaded');
            }
        }
    }




var staticInfoImgObj={
		"uploadId":"staticInfoImage",
		"uploadType":"staticInfoImage",
		"buttonType":"text",//按钮类型  img或text
		"buttonText":"选择图片",//如果按钮类型为‘text’,则配置需要 显示的文本按钮显示内容
		"fileObjName":"Filedata",//传递到后台的参数名称
		"fileSizeLimit":"1MB",//上传文件的大小限制 ，如果为整数型则表示以KB为单位的大小，如果是字符串，则可以使用(B, KB, MB, or GB)为单位，比如’2MB’
		"fileTypeExts":"*.jpg;*.png;*.jpge;*.gif",//可上传文件类型限制
		"uploadImgMethod":"staticInfoImgSuccess"//上传完成后回调名，会给回调方法传递fileObj
		};
createUploadify(staticInfoImgObj);
function staticInfoImgSuccess(serverDate){
	$("#staticInfoImageShow").attr("src",serverDate.resultMap.imageUrl);
	$("#staticInfoImage").val(serverDate.resultMap.fileNameNew);
	$("#licensePath").val(serverDate.resultMap.fileNameNew);
	$("#deleteStaticInfoImageBtn").show();
	return;
}
function deleteStaticInfoImg(){
	$("#staticInfoImageShow").attr("src","/images/default.jpg");
	$("#staticInfoImage").val("");
	$("#licensePath").val("");
	$("#deleteStaticInfoImageBtn").hide();
}
function getupdateImg(imgname){
   if(imgname!=""&&imgname!=null&&imgname!=undefined){
    $("#staticInfoImageShow").attr("src",serverPath+"/GetImage?bt=staticInfoImage&fn="+imgname);
	$("#staticInfoImage").val(imgname);
	$("#licensePath").val(imgname);
	$("#deleteStaticInfoImageBtn").show();
   }else{
   deleteStaticInfoImg();
   }
}
function showRowImg(img){
  $('#dg').datagrid('selectRow', img);
  var row = $('#dg').datagrid('getSelected');
  var imgpath=row.licensePath;
  if(imgpath!=""&&imgpath!=null&&imgpath!=undefined){
  $("#showCompanyImageId").attr("src",serverPath+"/GetImage?bt=staticInfoImage&fn="+imgpath);
  $('#showImageId').window('open');
   }else{
   top.window.showMessage("该公司还未上传营业执照！");
   }
}

function detail(comID){
    $.ajax({
    method:"post",
    url:'/v1/api0/company/detail',
    data:{companyID:comID},
    success:function(data){
        var company = data.body;
        $('#detail_name').text(company.name);
        $('#detail_socialCode').text(company.socialCode);
        $('#detail_address').text(company.address);
        $('#detail_leaderName').text(company.leaderName);
        $('#detail_mobile').text(company.mobile);
        $('#detail_email').text(company.email);
        $('#detail_bankCardNumber').text(company.bankCardNumber);
        $('#detail_alipayAccount').text(company.alipayAccount);
        $('#detail_wechatAccount').text(company.wechatAccount);
        <!--$('#detail_remark').text(company.remarks);-->
        $("#detail_license").attr("src",serverPath+"/GetImage?bt=staticInfoImage&fn="+company.licensePath);
        var createtime = company.createTime;
        createtime = new Date(createtime);
        var year = createtime.getFullYear()+'年';
        var month = createtime.getMonth()+1+'月';
        var date = createtime.getDate()+'日';
        $('#detail_createTime').text([year,month,date].join('-'));
    }
    });
$('#showDetail').window('open');
}

</script>
</body>
</html>
